//
//  HDBrowserHelper.m
//  leek-web_Example
//
//  Created by 吴非 on 2022/1/12.
//  Copyright © 2022 懒虫. All rights reserved.
//

#import "HDBrowserHelper.h"

@interface HDBrowserHelper()

/// 微x跳转
@property (nonatomic, copy, readwrite) NSString* schemeURLWeixin;
/// 微x跳转#关键词
@property (nonatomic, copy, readwrite) NSString* schemeURLWeixinKeyword;
/// 宝x跳转
@property (nonatomic, copy, readwrite) NSString* schemeURLAlipay;
/// 授权域名
@property (nonatomic, copy, readwrite) NSString* schemeURLDomain;
/// 默认重定向地址
@property (nonatomic, copy, readwrite) NSString* redirectURL;

@end

@implementation HDBrowserHelper


+ (instancetype)defaultService {
    static HDBrowserHelper *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[HDBrowserHelper alloc]init];
        
    });
    return _sharedClient;
}

/// 授权域名
+(void)renderDomain:(NSString*)tmpURL {
    [[HDBrowserHelper defaultService] setSchemeURLDomain:tmpURL];
}
/// 配置某信
+(void)renderWeixin:(NSString*)tmpURL {
    [[HDBrowserHelper defaultService] setSchemeURLWeixin:tmpURL];
}
/// 配置微信#关键词拼接
+(void)renderWeixinKeywords:(NSString*)kwd {
    [[HDBrowserHelper defaultService] setSchemeURLWeixinKeyword:kwd];
}

/// 配置某宝
+(void)renderAlipay:(NSString*)tmpURL {
    [[HDBrowserHelper defaultService] setSchemeURLAlipay:tmpURL];
}

/// 重定向地址
+(void)redirectURL:(NSString*)tmpURL {
    [[HDBrowserHelper defaultService] setRedirectURL:tmpURL];
}

/// 检查重定向地址
+(BOOL)checkRedirectURL:(NSString*)rURL {
    return [[[HDBrowserHelper defaultService] redirectURL] isEqualToString:rURL];
}


/// 解析某宝返回重定向地址
+(NSString* )analysisAlipayURL:(NSString*)absoluteUrl {
    /// 重定向地址 订单列表
    NSRange ruRange = [absoluteUrl rangeOfString:@"return_url="];
    if(ruRange.location != NSNotFound) {
        NSString* rbUrl = [absoluteUrl substringFromIndex:ruRange.location + ruRange.length];
        /// 检查是否末尾
        if([rbUrl rangeOfString:@"&"].location != NSNotFound) {
            NSRange xmRange = [rbUrl rangeOfString:@"&"];
            NSString* xbUrl = [rbUrl substringToIndex:xmRange.location];
            NSLog(@"重定向地址:%@", xbUrl);
            return xbUrl;
        } else {
            return rbUrl;
        }
    } else {
        NSLog(@"no");
    }
    return @"";
}

/// 解析某微返回重定向地址
+(NSString* )analysisWeixinURL:(NSString*)absoluteUrl {
    /// 重定向地址 订单列表
    NSRange ruRange = [absoluteUrl rangeOfString:@"redirect_url="];
    if(ruRange.location != NSNotFound) {
        NSString* rbUrl = [absoluteUrl substringFromIndex:ruRange.location + ruRange.length];
        /// 检查是否末尾
        if([rbUrl rangeOfString:@"&"].location != NSNotFound) {
            NSRange xmRange = [rbUrl rangeOfString:@"&"];
            NSString* xbUrl = [rbUrl substringToIndex:xmRange.location];
            NSLog(@"重定向地址:%@", xbUrl);
            return xbUrl;
        } else {
            return rbUrl;
        }
    } else {
        NSLog(@"no");
    }
    return @"";
}


/// 获取变量
-(NSString*)currentDomainURL {
    return self.schemeURLDomain;
}
/// 获取变量
-(NSString*)currentAlipayURL {
    return self.schemeURLAlipay;
}
/// 获取变量#支持多渠道#微信支付
-(NSString*)currentWeixinURL {
    if ([[self schemeURLWeixinKeyword] isEqualToString:@""]) {
        return self.schemeURLWeixin;
    } else {
        return  [NSString stringWithFormat:@"%@.%@", [self schemeURLWeixinKeyword], [self schemeURLDomain]];
    }
}


/// 超级处理微
+ (BOOL)handleOpenURL:(NSURL *)url {
    
    NSString* absoluteURL = url.absoluteString;
    // 获取某信
    if ([absoluteURL hasPrefix:[[HDBrowserHelper defaultService] currentWeixinURL]]) {
        NSLog(@"收到微信回调用");
        [[[HDBrowserHelper defaultService] delegate] weixinResponse:0 message:[[HDBrowserHelper defaultService] redirectURL]];
        return YES;
    }
    
    // safepay是支付宝H5支付的回调host,
    if ([absoluteURL hasPrefix:[[HDBrowserHelper defaultService] currentAlipayURL]] || [absoluteURL isEqualToString:@"safepay"]) {
        NSLog(@"收到支付宝回调用");
        [[[HDBrowserHelper defaultService] delegate] alipayResponse:0 message:[[HDBrowserHelper defaultService] redirectURL]];
        return YES;
    }
    
    return YES;
}

/// 清理缓存
+(void)removeCache {
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

@end
